﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Mission;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.mission;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Command.mission
{
    partial class SangoMissionHandler
    {
        public mission_mainsubmit mission_mainsubmit(long serialcode)
        {
            string userCode = SangoManager.Authentication();

            mission_mainsubmit response = ReflectionManager.CreatePojo<mission_mainsubmit>();
            response.Succeed = false;


            // 获取用户任务

            Entities.Mis_Mainmission mainMission = MissionHelper.getMainMission(serialcode);

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            long cashvalue = MissionFormular.getMainmissionCashprize(mainMission);
            if (!UsrProfileDao.tryIncreaseCashvalue(profile, cashvalue))
            {
                response.Message = "银币达到上线，无法增加";
                return response;
            }

            List<Entities.Usr_Mainmission> missions = MissionDao.getUsrMainMissions(userCode);

            Entities.Usr_Mainmission item = MissionDao.trySearchMainMission(missions, serialcode);

            if (item == null)
            {
                response.Message = "任务不存在";
                return response;
            }

            if (item.Missionstatus != GeneralBillStatusCollection.FINISH)
            {
                response.Message = "任务尚未完成";
                return response;
            }

            MissionDao.commitMainMission(item);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(item);

                List<Entities.Usr_Mainmission> list = MissionDao.searchOpenMissions(profile.Usercode, profile.Nickname, serialcode);
                MainMissionFactory.handle(userCode, list);
                command.Insert(list.ToArray());

                command.Update(profile);

                command.Commit();
            });


            response.profile = UsrProfileFacade.ToProfile(profile);
            response.list = MissionFacade.ToMissions(MissionDao.getUsrMainMissions(userCode));
            response.Succeed = true;
            return response;
        }


    }
    public interface mission_mainsubmit : ISangoResponse
    {
        player_profile profile { get;set;}
        List<mission_main> list { get;set;}
    }
}
